user191776
user191776

Reputation:

A function in a pthread

Can I call another function in a thread runner function, called by a pthread_create()? Are there any restrictions on such functions?

Upvotes: 3

Views: 1474

Answers (3)

Michael Shopsin
Michael Shopsin

Reputation: 2138

You can call any function you want from a thread, but C does not automatically synchronize values. If a function uses global variables or static variables then you may get some bad surprises when you call it in multi-threaded code.

Upvotes: 1

Starkey
Starkey

Reputation: 9781

You can call any function from a runner function. BUT, you should make sure that any function in a multi-threaded system is protected with mutexes correctly.

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490048

Yes, you can (and doing so is fairly frequent). The main restriction is that you need to synchronize threads when two or more access the same data (at least if there's any chance that any of them might modify that data).

Upvotes: 6

Related Questions