[英]pass pthread_mutex_t to function
Is it ok to pass pthread lock to a function? 可以将pthread锁传递给函数吗? the reason I'm asking for this is because I have a number of threads, let's say 10, acting as consumers, and 2 producers, each consumer thread has it's own linked list(buffer), the producers will send data to one of these threads based on a specific rules, so sometimes producer 1 sends to consumer 1 and then to consumer 2 and then back to consumer 1 so on.
我要这个的原因是因为我有多个线程,比如说有10个充当消费者,还有2个生产者,每个消费者线程都有自己的链表(缓冲区),生产者将向其中一个发送数据线程基于特定的规则,因此有时生产者1发送给使用者1,然后发送给使用者2,然后又返回给使用者1,依此类推。
So I'm thinking of this, 所以我在想这个
when producer 1 want to send to consumer 5, 当生产者1想要发送给消费者5时,
please note that the name of lock is shared in the main() 请注意,锁的名称在main()中共享
pthread_mutex_t lock[10];
I'm using gcc on linux, and please let me know if the question isn't clear as I have a terrible way of describing things. 我在Linux上使用gcc,请问问题是否明确,因为我有一种糟糕的描述方式。
You can. 您可以。 just make sure to pass the address of the mutex.
只要确保传递互斥体的地址即可。 You can't copy it.
您无法复制。
You can do a good think. 您可以考虑一下。 you create an other c file and you insert:
您创建另一个c文件并插入:
pthread_mutex_t lock[10];
type_t *produce(int tid) {
if (tid == {producer_1}) {
//call function linked-list to share the data (IPC) and pass lock[5] to this function, in this case it will be lock 5
}
...
}
void consume(type_t* data, int tid) {
....
}
in this manner you centralize your processing to make your program more readable and simple. 通过这种方式,您可以集中处理,使程序更具可读性和简单性。 in your threads you just apply
produce
and consume
with producer/consumer ids 在您的线程中,您只需使用生产者/消费者ID来应用
produce
和consume
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.