有没有办法确定只有一个线程执行特定的功能?
for (i = startnotfirst; i < end; i++) {
gl_rand = (float) lrand48();
fprintf(stderr, "\nTHREADING - #ID: %d - THIS IS MY RAND: %f", *mytid,rand);
to_open = (rand / (float) INT_MAX) < (points->p[i].cost / z);
if (to_open) {
//printf("\nRANDOM: %f \nINT_MAX: %d \npointsDistance(cost): %f \nZ: %f \n\n RESULT: %f < %f\n\n\n",rand ,INT_MAX,points->p[1].cost , z,(rand / (float) INT_MAX),(points->p[i].cost / z));
fprintf(stderr, "\nTHREADING - #ID: %d - im working...", *mytid);
t_kcenter++;
for (int k = start; k < end; k++) {
float distance = dist(points->p[i], points->p[k], points->dim);
//If distance is smaller add it to that cluster.
float new_cost = distance * points->p[k].weight;
if (new_cost < points->p[k].cost) {
points->p[k].cost = new_cost;
points->p[k].assign = i;
}
}
}
}
我希望gl_rand仅由一个线程执行,其他人通过更改global variable
或broadcasting
来了解新的rand值。
此外,线程必须在新号码出现之前结束其作业iteration body
!
有什么想法吗?谢谢!
答案 0 :(得分:2)
pthread_cond_wait()
和pthread_cond_broadcast()
与pthread_mutex_lock()
结合使用
pthread_mutex_unlock()
,
pthread_cond_wait()
和
pthread_cond_broadcast()
函数是实现所需功能的关键。
但是,需要非常小心才能使谓词正确。
/*
** Objective: N threads cooperate on M cycles or iterations of a task.
** A new random number is needed for each cycle, but all threads must
** use the same random number on each cycle.
** Any thread may evaluate the new random number.
*/
#include <assert.h>
#include <errno.h>
#include <pthread.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "stderr.h"
#ifndef NUM_THREADS
#define NUM_THREADS 3
#endif
#ifndef NUM_CYCLES
#define NUM_CYCLES 5
#endif
enum { MAX_THREADS = NUM_THREADS };
enum { MAX_CYCLES = NUM_CYCLES };
static pthread_mutex_t mtx_waiting = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cnd_waiting = PTHREAD_COND_INITIALIZER;
static int num_waiting = 0;
static int cycle = -1;
static float gl_rand = 0;
static long gl_long = 0;
static float next_iteration_random_number(int tid, int iteration)
{
pthread_mutex_lock(&mtx_waiting);
assert(cycle == iteration || cycle == iteration - 1);
num_waiting++;
printf("-->> TID %d, I = %d (C = %d, W = %d)\n",
tid, iteration, cycle, num_waiting);
while (cycle != iteration && num_waiting != MAX_THREADS)
{
assert(num_waiting > 0 && num_waiting <= MAX_THREADS);
printf("-CW- TID %d, I = %d (C = %d, W = %d)\n",
tid, iteration, cycle, num_waiting);
pthread_cond_wait(&cnd_waiting, &mtx_waiting);
}
assert(cycle == iteration || num_waiting == MAX_THREADS);
printf("---- TID %d, I = %d (C = %d, W = %d)\n",
tid, iteration, cycle, num_waiting);
if (cycle != iteration)
{
gl_long = lrand48();
gl_rand = (float)gl_long;
num_waiting = 0;
cycle = iteration;
printf("---- TID %d generates cycle %d: L = %ld, F = %g\n",
tid, cycle, gl_long, gl_rand);
pthread_cond_broadcast(&cnd_waiting);
}
printf("<<-- TID %d, I = %d (C = %d, W = %d) L = %ld, F = %g\n",
tid, iteration, cycle, num_waiting, gl_long, gl_rand);
pthread_mutex_unlock(&mtx_waiting);
return gl_rand;
}
static void *thread_function(void *vp)
{
int tid = (int)(uintptr_t)vp; // Thuggish!
for (int i = 0; i < MAX_CYCLES; i++)
{
float f = next_iteration_random_number(tid, i);
printf("TID %d at work: I = %d, F = %g\n", tid, i, f);
fflush(stdout);
struct timespec rq;
rq.tv_sec = 0;
rq.tv_nsec = (((gl_long & 0xFF) + (0xF * tid))) % 200 * 50000000;
assert(rq.tv_nsec >= 0 && rq.tv_nsec < 10000000000);
nanosleep(&rq, 0);
}
return 0;
}
int main(int argc, char **argv)
{
err_setarg0(argv[0]);
assert(argc == 1);
pthread_t thread[MAX_THREADS];
for (int i = 0; i < MAX_THREADS; i++)
{
int rc = pthread_create(&thread[i], 0, thread_function, (void *)(uintptr_t)i);
if (rc != 0)
{
errno = rc;
err_syserr("failed to create thread %d", i);
}
}
for (int i = 0; i < MAX_THREADS; i++)
{
void *vp;
int rc = pthread_join(thread[i], &vp);
if (rc != 0)
{
errno = rc;
err_syserr("Failed to join TID %d", i);
}
printf("TID %d returned %p\n", i, vp);
}
return 0;
}
来自GitHub的来源。 libsoq
中的图书馆代码。开始err_
的例程在标题stderr.h
中声明,支持代码在stderr.c
中。这些大大简化了错误报告。
main()
功能微不足道;它启动5个线程,然后等待加入5个线程。唯一棘手的一点是将线程号作为参数传递给线程函数。代码将整数值转换为uintptr_t
,然后将其强制转换为void *
;被调用的函数撤消了这个序列
每线程功能并不复杂。它从其参数中收集线程号,然后进入循环以迭代所需的循环次数。在每次迭代中,它将迭代次数(和线程数)传递给next_iteration_random_number()
,后者协调随机数生成。线程打印数据,然后使用nanosleep()
睡眠时间不到1秒钟的时间。
有趣的代码在next_iteration_random_number()
。首先,线程锁定控制共享信息的互斥锁。然后它增加等待线程的数量。如果当前循环与它正在处理的迭代不同,并且等待线程的数量不等于线程的总数,则该线程调用pthread_cond_wait()
进入休眠状态直到广播到。当线程离开循环时(通过在广播之后唤醒,或者因为它从未进入循环),线程会查看当前循环是否是它期望的迭代。如果没有,它必须是尝试此循环的最后一个线程 - 因此它生成随机数并进行内务处理以确保其他线程将知道发生了什么,
然后它使用pthread_cond_broadcast()
来发出唤醒所有睡眠线程的信号。他们实际上无法醒来;当前线程仍然保留互斥锁。它报告它正在执行的操作并解锁互斥锁,并返回随机数。其他线程检测到循环编号与他们等待的迭代次数相同,因此他们可以分别打印信息,解锁互斥锁,并返回数字。
关键是每个线程必须知道它期望处理哪个迭代,并且当它还不是当前迭代时必须适当地休眠。代码中有很多断言和打印操作,以帮助理解正在发生的事情。
完全独立,因为lrand48()
返回long
(尽管值为[0,2 31 ],所以它甚至是32位值当long
是64位类型时),这意味着它很有可能返回无法在float
中准确表示的值,因此gl_rand = (float)lrand48();
代码会从问题中剪切掉是可疑的。 (示例代码还会在long
中记录gl_long
值并偶尔使用它。)
代码在运行macOS Sierra 10.12.3并使用GCC 6.3.0并且编译选项设置为繁琐的Mac上完全编译:
$ gcc -O3 -g -I../../inc -std=c11 -Wall -Wextra -Werror -Wmissing-prototypes \
> -Wstrict-prototypes -Wold-style-definition pthrd37.c -o pthrd37 \
> -L../../lib -lsoq
$
程序配置有3个线程和5个周期:
-->> TID 0, I = 0 (C = -1, W = 1)
-CW- TID 0, I = 0 (C = -1, W = 1)
-->> TID 2, I = 0 (C = -1, W = 2)
-CW- TID 2, I = 0 (C = -1, W = 2)
-->> TID 1, I = 0 (C = -1, W = 3)
---- TID 1, I = 0 (C = -1, W = 3)
---- TID 1 generates cycle 0: L = 851401618, F = 8.51402e+08
<<-- TID 1, I = 0 (C = 0, W = 0) L = 851401618, F = 8.51402e+08
TID 1 at work: I = 0, F = 8.51402e+08
---- TID 0, I = 0 (C = 0, W = 0)
<<-- TID 0, I = 0 (C = 0, W = 0) L = 851401618, F = 8.51402e+08
TID 0 at work: I = 0, F = 8.51402e+08
---- TID 2, I = 0 (C = 0, W = 0)
<<-- TID 2, I = 0 (C = 0, W = 0) L = 851401618, F = 8.51402e+08
TID 2 at work: I = 0, F = 8.51402e+08
-->> TID 1, I = 1 (C = 0, W = 1)
-CW- TID 1, I = 1 (C = 0, W = 1)
-->> TID 0, I = 1 (C = 0, W = 2)
-CW- TID 0, I = 1 (C = 0, W = 2)
-->> TID 2, I = 1 (C = 0, W = 3)
---- TID 2, I = 1 (C = 0, W = 3)
---- TID 2 generates cycle 1: L = 1804928587, F = 1.80493e+09
<<-- TID 2, I = 1 (C = 1, W = 0) L = 1804928587, F = 1.80493e+09
TID 2 at work: I = 1, F = 1.80493e+09
---- TID 1, I = 1 (C = 1, W = 0)
<<-- TID 1, I = 1 (C = 1, W = 0) L = 1804928587, F = 1.80493e+09
TID 1 at work: I = 1, F = 1.80493e+09
---- TID 0, I = 1 (C = 1, W = 0)
<<-- TID 0, I = 1 (C = 1, W = 0) L = 1804928587, F = 1.80493e+09
TID 0 at work: I = 1, F = 1.80493e+09
-->> TID 2, I = 2 (C = 1, W = 1)
-CW- TID 2, I = 2 (C = 1, W = 1)
-->> TID 1, I = 2 (C = 1, W = 2)
-CW- TID 1, I = 2 (C = 1, W = 2)
-->> TID 0, I = 2 (C = 1, W = 3)
---- TID 0, I = 2 (C = 1, W = 3)
---- TID 0 generates cycle 2: L = 758783491, F = 7.58783e+08
<<-- TID 0, I = 2 (C = 2, W = 0) L = 758783491, F = 7.58783e+08
TID 0 at work: I = 2, F = 7.58783e+08
---- TID 2, I = 2 (C = 2, W = 0)
<<-- TID 2, I = 2 (C = 2, W = 0) L = 758783491, F = 7.58783e+08
TID 2 at work: I = 2, F = 7.58783e+08
---- TID 1, I = 2 (C = 2, W = 0)
<<-- TID 1, I = 2 (C = 2, W = 0) L = 758783491, F = 7.58783e+08
TID 1 at work: I = 2, F = 7.58783e+08
-->> TID 0, I = 3 (C = 2, W = 1)
-CW- TID 0, I = 3 (C = 2, W = 1)
-->> TID 2, I = 3 (C = 2, W = 2)
-CW- TID 2, I = 3 (C = 2, W = 2)
-->> TID 1, I = 3 (C = 2, W = 3)
---- TID 1, I = 3 (C = 2, W = 3)
---- TID 1 generates cycle 3: L = 959030623, F = 9.59031e+08
<<-- TID 1, I = 3 (C = 3, W = 0) L = 959030623, F = 9.59031e+08
TID 1 at work: I = 3, F = 9.59031e+08
-->> TID 1, I = 4 (C = 3, W = 1)
-CW- TID 1, I = 4 (C = 3, W = 1)
---- TID 0, I = 3 (C = 3, W = 1)
<<-- TID 0, I = 3 (C = 3, W = 1) L = 959030623, F = 9.59031e+08
TID 0 at work: I = 3, F = 9.59031e+08
---- TID 2, I = 3 (C = 3, W = 1)
<<-- TID 2, I = 3 (C = 3, W = 1) L = 959030623, F = 9.59031e+08
TID 2 at work: I = 3, F = 9.59031e+08
-->> TID 0, I = 4 (C = 3, W = 2)
-CW- TID 0, I = 4 (C = 3, W = 2)
-->> TID 2, I = 4 (C = 3, W = 3)
---- TID 2, I = 4 (C = 3, W = 3)
---- TID 2 generates cycle 4: L = 684387517, F = 6.84388e+08
<<-- TID 2, I = 4 (C = 4, W = 0) L = 684387517, F = 6.84388e+08
TID 2 at work: I = 4, F = 6.84388e+08
---- TID 1, I = 4 (C = 4, W = 0)
<<-- TID 1, I = 4 (C = 4, W = 0) L = 684387517, F = 6.84388e+08
TID 1 at work: I = 4, F = 6.84388e+08
---- TID 0, I = 4 (C = 4, W = 0)
<<-- TID 0, I = 4 (C = 4, W = 0) L = 684387517, F = 6.84388e+08
TID 0 at work: I = 4, F = 6.84388e+08
TID 0 returned 0x0
TID 1 returned 0x0
TID 2 returned 0x0
请注意,每个线程碰巧至少生成一次随机数。这不是可以保证的东西,但它表明没有任何一个线程具有特权。
pthread_once()
此答案的第一个版本提到并说明了pthread_once()
电话。这不是实际需要的,但是
它可能在其他情况下有用。
假设你有一个足够的POSIX兼容系统,如果你只想要一个线程做某事,但是哪个线程无关紧要,那么我相信你正在寻找pthread_once()
。
#include <pthread.h>
#include <stdlib.h>
static pthread_once_t once_only = PTHREAD_ONCE_INIT;
static float gl_rand = 0;
static void pt_once(void)
{
gl_rand = (float)lrand48();
}
void *thread_function(void *vp)
{
pthread_once(&once_only, pt_once);
…rest of the code…
return 0;
}