标签: thread-synchronization

两个线程可以使用相同的线程程序吗?

当使用CreateThread()时,两个线程是否可以使用单个函数"ThreadProc"作为其线程过程?

HANDLE thread1= CreateThread( NULL, //Choose default security
                              0, //Default stack size
                              (LPTHREAD_START_ROUTINE)&ThreadProc,
                              //Routine to execute. I want this routine to be different each time as I want each  thread to perform a different functionality.
                              (LPVOID) &i, //Thread parameter
                              0, //Immediately run the thread
                              &dwThreadId //Thread Id
                            ) 
HANDLE thread2= CreateThread( NULL, //Choose default security
                              0, //Default stack size
                              (LPTHREAD_START_ROUTINE)&ThreadProc,
                              //Routine to execute. I want this routine to be different each time as I want each  thread to perform a …
Run Code Online (Sandbox Code Playgroud)

c windows winapi multithreading thread-synchronization

0
推荐指数
2
解决办法
1231
查看次数

线程同步:等待两个bool变量

我想bool在一个线程中等待两个变量为真.他们在不同的地方改变.我可以在我的项目中使用boost,但不能在C++ 11中使用boost.我确实找到了有关如何使用互斥锁和条件变量的信息,但我不确定是否可以等待两个互斥锁.这是我的程序的一些伪代码.

bool job1_dataready, job2_dataready;

//t1:
void job1()
{
   //do stuff
   job1_dataready = true;
}

//t2:
void job2()
{
   //do stuff
   job2_dataready= true;
}

main()
{
 boost::thread t1(job1);
 boost::thread t1(job2);

 if(job1_dataready&& job2_dataready)
 {
    //do stuff with data from both jobs
 }

}
Run Code Online (Sandbox Code Playgroud)

c++ multithreading thread-safety thread-synchronization

0
推荐指数
1
解决办法
118
查看次数

应该何时声明方法同步?

以下是一个简单的工作程序,它使用两个线程来打印计数器:

public class SynchronizedCounter implements Runnable {

    private static int i = 0;

    public void increment() { i++; }
    public int getValue() { return i; }  

    @Override
    public void run() {
        for( int i = 0; i < 5; i++ ) {
            synchronized( this ) {
                increment();
                System.out.println( Thread.currentThread().getName() + " counter: " + this.getValue() );
            }
        }
    }

    public static void main( String[] args ) {

        ExecutorService executorService = Executors.newFixedThreadPool( 2 );
        SynchronizedCounter synchronizedCounter = new SynchronizedCounter();
        executorService.submit( synchronizedCounter …
Run Code Online (Sandbox Code Playgroud)

java concurrency multithreading thread-synchronization

0
推荐指数
1
解决办法
72
查看次数

在互斥锁中调用sleep()有什么缺点?

例如:

pthread_mutex_lock();
//Do something
sleep(1);                //causes issues waiting while holding lock

pthread_mutex_unlock();
Run Code Online (Sandbox Code Playgroud)

如果我们不想在互斥锁中使用sleep,那么解决方案是什么?

c multithreading mutex thread-synchronization coverity

-2
推荐指数
1
解决办法
603
查看次数

在那些简单的情况下使用 C# 锁有用吗?

在多线程环境中,锁定对线程敏感的资源很重要。我经常假设集合等是线程不安全的,具体取决于 MS 文档,但简单类型是否也是线程敏感的?

让我们举个例子。锁定 int 属性访问是否有用,例如

public int SomeProperty
{
    get
    {
        lock (_lock)
        {
             return _value;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

或者是一个足够普通的吸气剂,即

public int SomeProperty => _value;
Run Code Online (Sandbox Code Playgroud)

据我了解,一个简单的字段读取是线程安全的,但我仍然在网上和一些代码库中看到第一个例子。

第二个问题,单行指令中的值是顺序读取还是同时读取?换句话说,我这样做时是否需要锁定

public TimeSpan GetSomeExampleValue()
{
    lock (_lock)
    {
        return _dateTime1 - _dateTime2;
    }
}
Run Code Online (Sandbox Code Playgroud)

或者我可以简单地做

public TimeSpan GetSomeExampleValue()
{
    return _dateTime1 - _dateTime2;
}
Run Code Online (Sandbox Code Playgroud)

c# multithreading locking thread-safety thread-synchronization

-2
推荐指数
1
解决办法
62
查看次数