linux c pthread pthread_create pthread_mutex_init pthread 自定义参数 例程

linux c pthread 例程

biometric-authenticationd.c

静态初始化了 1023个pthread_mutex_t 变量。
pthread_create 的句柄是一个局部临时变量

#define BIO_DRVID_DYNAMIC_MAX		1023
#define MAX_DEVICES	BIO_DRVID_DYNAMIC_MAX
pthread_mutex_t dev_dbus_mutex[MAX_DEVICES];

	int err;
	pthread_t thr;

	err = pthread_create(&thr, NULL, (void *)monitor_usb_hotplug, NULL);

	if (err != 0) {
		bio_print_error(_("Can't create thread: %s\n"), strerror(err));
	} else {
		bio_print_info(_("New thread created: %s\n"), strerror(err));
	}

	for (i = 0; i < MAX_DEVICES; i++) {
		pthread_mutex_init(&dev_dbus_mutex[i], NULL);
		gettimeofday(&timestamp_dev_status[i], NULL);
		gettimeofday(&timestamp_ops_result[i], NULL);
		gettimeofday(&timestamp_notify_mid[i], NULL);
	}

1

void monitor_usb_hotplug(void* para) {
	int rc =0;

	while (1) {
			rc = libusb_handle_events (NULL);
			if (rc < 0)
				bio_print_error(_("libusb_handle_events failed: %s\n"), libusb_error_name(rc));
		}
}
pthread_mutex_trylock 与pthread_mutex_unlock
	if (pthread_mutex_trylock(&dev_dbus_mutex[drvid]) != 0) {
		biometric_complete_get_feature_list(object, invocation, 0, flv_null);
        bio_print_debug("%s exit 2\n",__func__);
		return;
	}
	g_variant_builder_unref(_builder);
	g_variant_unref(flv_null);

	feature_info * flist = NULL;
	flist = bio_ops_get_feature_list(dev, uid, idx_start, idx_end);
	pthread_mutex_unlock(&dev_dbus_mutex[drvid]);

自定义线程参数:malloc 与 free

pthread_create前 malloc 分配内存
然后在 线程里面 free这些内存

static gboolean gdbus_get_feature_list(Biometric *object,
							 GDBusMethodInvocation *invocation,
							 const int drvid, const int uid,
							 const int idx_start, const int idx_end)
{
	int err = 0;
	pthread_t thr;

	GSList *argv = NULL;
	Biometric **p0;
	GDBusMethodInvocation **p1;
	int *p2;
	int *p3;
	int *p4;
	int *p5;

	p0 = malloc(sizeof(void *));
	memcpy(p0, &object, sizeof(void *));
	argv = g_slist_append(argv, p0);

	p1 = malloc(sizeof(void *));
	memcpy(p1, &invocation, sizeof(void *));
	argv = g_slist_append(argv, p1);

	p2 = malloc(sizeof(const int));
	memcpy(p2, &drvid, sizeof(const int));
	argv = g_slist_append(argv, p2);

	p3 = malloc(sizeof(const int));
	memcpy(p3, &uid, sizeof(const int));
	argv = g_slist_append(argv, p3);

	p4 = malloc(sizeof(const int));
	memcpy(p4, &idx_start, sizeof(const int));
	argv = g_slist_append(argv, p4);

	p5 = malloc(sizeof(const int));
	memcpy(p5, &idx_end, sizeof(const int));
	argv = g_slist_append(argv, p5);

	err = pthread_create(&thr, NULL, (void *)gdbus_get_feature_list_pt, argv);

	if (err != 0) {
		bio_print_error(_("Can't create thread: %s\n"), strerror(err));
	}

	return true;
}

void gdbus_get_feature_list_pt(GSList * argv) {
	int count = 0;

	Biometric **p0;
	GDBusMethodInvocation **p1;
	int *p2;
	int *p3;
	int *p4;
	int *p5;
    bio_print_debug("%s enter\n",__func__);
	p0 = g_slist_nth_data(argv, 0);
	p1 = g_slist_nth_data(argv, 1);
	p2 = g_slist_nth_data(argv, 2);
	p3 = g_slist_nth_data(argv, 3);
	p4 = g_slist_nth_data(argv, 4);
	p5 = g_slist_nth_data(argv, 5);

	Biometric *object = (Biometric *)*p0;
	GDBusMethodInvocation *invocation = (GDBusMethodInvocation *)*p1;
	int drvid = (int)*p2;
	int uid = (int)*p3;
	int idx_start = (int)*p4;
	int idx_end = (int)*p5;


	bio_dev * dev = get_dev_by_drvid(drvid);
	GVariant * flv_null;
	GVariantBuilder* _builder = g_variant_builder_new(G_VARIANT_TYPE("av"));
	flv_null = g_variant_builder_end(_builder);

	if (dev == NULL) {
		biometric_complete_get_feature_list(object, invocation, 0, flv_null);

        bio_print_debug("%s exit 1\n",__func__);
		return;
	}

	if (pthread_mutex_trylock(&dev_dbus_mutex[drvid]) != 0) {
		biometric_complete_get_feature_list(object, invocation, 0, flv_null);
        bio_print_debug("%s exit 2\n",__func__);
		return;
	}
	g_variant_builder_unref(_builder);
	g_variant_unref(flv_null);

	feature_info * flist = NULL;
	flist = bio_ops_get_feature_list(dev, uid, idx_start, idx_end);
	pthread_mutex_unlock(&dev_dbus_mutex[drvid]);

	int ops_result = bio_get_ops_result(dev);
	if (ops_result != OPS_GET_FLIST_SUCCESS)
	{
		_builder = g_variant_builder_new(G_VARIANT_TYPE("av"));
		flv_null = g_variant_builder_end(_builder);
		biometric_complete_get_feature_list(object, invocation,
											-RetOpsError, flv_null);

        bio_print_debug("%s exit 3\n",__func__);                                            
		return;
	}

	// 构建特征信息列表
	feature_info * l = flist;
	GVariant * flv;
	_builder = g_variant_builder_new(G_VARIANT_TYPE("av"));

	while (l != NULL) {
		GVariant * _item = g_variant_new("(iisis)", l->uid, l->biotype,
										 bio_new_string(l->driver),
										 l->index,
										 bio_new_string(l->index_name));
		g_variant_builder_add(_builder, "v", _item);

		l = l->next;
		count += 1;
	}
	flv = g_variant_builder_end(_builder);
	g_variant_builder_unref(_builder);

	if (flist != NULL)
		bio_sto_free_feature_info(flist);

	biometric_complete_get_feature_list(object, invocation, count, flv);

	free(p0);
	free(p1);
	free(p2);
	free(p3);
	free(p4);
	free(p5);
	g_slist_free(argv);

    bio_print_debug("%s exit 4\n",__func__);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值