diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/pom.xml" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/pom.xml" index fe3a3e6..5ecbb5a 100644 --- "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/pom.xml" +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/pom.xml" @@ -13,6 +13,7 @@ stage-1 stage-2 stage-3 + stage-4 diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/stage-4/pom.xml" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/stage-4/pom.xml" new file mode 100644 index 0000000..5333673 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/stage-4/pom.xml" @@ -0,0 +1,32 @@ + + + + deep-in-java + com.segmentfault + 1.0-SNAPSHOT + + 4.0.0 + + stage-4 + pom + 「一入 Java 深似海 」系列 :: 第四期 + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + 11 + 11 + + + + + + stage-4-lesson-1 + + \ No newline at end of file diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/stage-4/stage-4-lesson-1/pom.xml" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/stage-4/stage-4-lesson-1/pom.xml" new file mode 100644 index 0000000..03659c0 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/stage-4/stage-4-lesson-1/pom.xml" @@ -0,0 +1,16 @@ + + + + stage-4 + com.segmentfault + 1.0-SNAPSHOT + + 4.0.0 + + stage-4-lesson-1 + 「一入 Java 深似海 」系列 :: 第四期 :: 第一节 + + + \ No newline at end of file diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/stage-4/stage-4-lesson-1/src/main/java/com/segmentfault/deep/in/java/concurrency/HelloWorldThreadDemo.java" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/stage-4/stage-4-lesson-1/src/main/java/com/segmentfault/deep/in/java/concurrency/HelloWorldThreadDemo.java" new file mode 100644 index 0000000..6e19324 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/stage-4/stage-4-lesson-1/src/main/java/com/segmentfault/deep/in/java/concurrency/HelloWorldThreadDemo.java" @@ -0,0 +1,24 @@ +package com.segmentfault.deep.in.java.concurrency; + +public class HelloWorldThreadDemo { + + public static void main(String[] args) throws Exception { + + // 创建 Java 线程 + // Java 线程对象和 JVM OS 线程并不是同一对象 + Thread t1 = new Thread(HelloWorldThreadDemo::helloWorld); + // 主线程 main 显示地启动子线程 + t1.start(); // pthread_create() + + // 等待线程执行结束 + t1.join(); // pthread_join() + + // 当线程 isAlive() 返回 false 时,JVM 线程已经消亡了(delete this) + System.out.printf("线程状态 : %s , 是否存活 : %s", t1.getState(), t1.isAlive()); + } + + static void helloWorld() { + System.out.printf("Thread[id : %d] - Hello World\n", + Thread.currentThread().getId()); + } +} diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/stage-4/stage-4-lesson-1/src/main/java/com/segmentfault/deep/in/java/concurrency/SynchronizationDemo.java" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/stage-4/stage-4-lesson-1/src/main/java/com/segmentfault/deep/in/java/concurrency/SynchronizationDemo.java" new file mode 100644 index 0000000..91cece8 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/deep-in-java/stage-4/stage-4-lesson-1/src/main/java/com/segmentfault/deep/in/java/concurrency/SynchronizationDemo.java" @@ -0,0 +1,50 @@ +package com.segmentfault.deep.in.java.concurrency; + +import java.util.concurrent.locks.Condition; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; + +public class SynchronizationDemo { + + // pthread_mutex_t lock; + static Lock lock = new ReentrantLock(); + + static volatile int counter = 0; + + public static void main(String[] args) throws Exception { + + // pthread_cond_t condition1; + Condition condition1 = lock.newCondition(); + + // 前提:Lock#lock() + // await() 和 signal() 或 signalAll() + + // 前提:synchronized(object) -> + // Object wait() 和 notify() 或 notifyAll(); + + synchronized (Object.class) { +// Object.class.wait(); + } + + Thread t1 = new Thread(SynchronizationDemo::addCounter); + Thread t2 = new Thread(SynchronizationDemo::addCounter); + t1.start(); + t2.start(); + + t1.join(); + t2.join(); + } + + private static void addCounter() { + lock.lock(); // pthread_mutex_lock() + // lock.tryLock() // pthread_mutex_trylock() + System.out.println(getThreadPrefix() + "Before Counter : " + counter); + counter++; + System.out.println(getThreadPrefix() + "After Counter : " + counter); + lock.unlock(); // pthread_mutex_unlock() + } + + private static String getThreadPrefix() { + return "Thread[" + Thread.currentThread().getId() + "] : "; + } +} diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes.sln" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes.sln" new file mode 100644 index 0000000..40f1d52 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes.sln" @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.438 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PosixThread_Attributes", "PosixThread_Attributes\PosixThread_Attributes.vcxproj", "{CFEF4222-68AC-4D9C-98D2-E0E2181277E4}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Debug|x64.ActiveCfg = Debug|x64 + {CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Debug|x64.Build.0 = Debug|x64 + {CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Debug|x86.ActiveCfg = Debug|Win32 + {CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Debug|x86.Build.0 = Debug|Win32 + {CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Release|x64.ActiveCfg = Release|x64 + {CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Release|x64.Build.0 = Release|x64 + {CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Release|x86.ActiveCfg = Release|Win32 + {CFEF4222-68AC-4D9C-98D2-E0E2181277E4}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {B1D908A1-8B3F-4C04-9913-124FC142A4A3} + EndGlobalSection +EndGlobal diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/PosixThread_Attributes.cpp" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/PosixThread_Attributes.cpp" new file mode 100644 index 0000000..0b34693 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/PosixThread_Attributes.cpp" @@ -0,0 +1,43 @@ +// PosixThread_HelloWorld.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 +// + +#include "pch.h" +#include +#include + +using namespace std; + + +int main() +{ + pthread_t t1; + + // 申明 POSIX Thread 属性变量 + pthread_attr_t attr; + // 申请线程栈大小(无符号整型) 512 K + size_t stack_size = 512 * 1000; + + // 初始化 pthread_attr_t + pthread_attr_init(&attr); + // 设置 pthread_attr_t 栈大小 -> 512 K + pthread_attr_setstacksize(&attr, stack_size); + + // 创建 t1 线程,并且将执行对象指向 void* helloWorld(void* ptr); + int result = pthread_create(&t1, &attr, helloWorld, NULL); + + pthread_join(t1, NULL); + // 销毁 pthread_attr_t attr + pthread_attr_destroy(&attr); + //线程退出 + pthread_exit(NULL); + + return EXIT_SUCCESS; +} + +void* helloWorld(void* ptr) { + pthread_t t = pthread_self(); + //printf("Thread - Hello World \n"); + cout << "Hello World" << endl; + return NULL; +} + diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/PosixThread_Attributes.vcxproj" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/PosixThread_Attributes.vcxproj" new file mode 100644 index 0000000..c5882e7 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/PosixThread_Attributes.vcxproj" @@ -0,0 +1,171 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {CFEF4222-68AC-4D9C-98D2-E0E2181277E4} + Win32Proj + PosixThreadAttributes + 10.0.17763.0 + + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + E:\software\dev\CPP\pthreads-w32-2-9-1\Pre-built.2\include;$(IncludePath) + E:\software\dev\CPP\pthreads-w32-2-9-1\Pre-built.2\dll\x86;$(LibraryPath) + + + true + + + false + + + false + + + + Use + Level3 + Disabled + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + E:\software\dev\CPP\pthreads-w32-2-9-1\Pre-built.2\lib\x86\pthreadVC2.lib;%(AdditionalDependencies) + + + + + Use + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + + + + + Use + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + true + true + + + + + Use + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + true + true + + + + + + + + Create + Create + Create + Create + + + + + + + \ No newline at end of file diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/PosixThread_Attributes.vcxproj.filters" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/PosixThread_Attributes.vcxproj.filters" new file mode 100644 index 0000000..9b6edfc --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/PosixThread_Attributes.vcxproj.filters" @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 头文件 + + + + + 源文件 + + + 源文件 + + + \ No newline at end of file diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/PosixThread_Attributes.vcxproj.user" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/PosixThread_Attributes.vcxproj.user" new file mode 100644 index 0000000..be25078 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/PosixThread_Attributes.vcxproj.user" @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/pch.cpp" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/pch.cpp" new file mode 100644 index 0000000..8eb50d0 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/pch.cpp" @@ -0,0 +1,5 @@ +// pch.cpp: 与预编译标头对应的源文件;编译成功所必需的 + +#include "pch.h" + +// 一般情况下,忽略此文件,但如果你使用的是预编译标头,请保留它。 diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/pch.h" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/pch.h" new file mode 100644 index 0000000..bd62e27 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Attributes/PosixThread_Attributes/pch.h" @@ -0,0 +1,15 @@ +// 入门提示: +// 1. 使用解决方案资源管理器窗口添加/管理文件 +// 2. 使用团队资源管理器窗口连接到源代码管理 +// 3. 使用输出窗口查看生成输出和其他消息 +// 4. 使用错误列表窗口查看错误 +// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 +// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件 + +#ifndef PCH_H +#define PCH_H + +void* helloWorld(void* ptr); +// TODO: 添加要在此处预编译的标头 + +#endif //PCH_H diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld.sln" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld.sln" new file mode 100644 index 0000000..086b867 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld.sln" @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.438 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PosixThread_HelloWorld", "PosixThread_HelloWorld\PosixThread_HelloWorld.vcxproj", "{F6804956-219B-4853-A162-6F85685868C5}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {F6804956-219B-4853-A162-6F85685868C5}.Debug|x64.ActiveCfg = Debug|x64 + {F6804956-219B-4853-A162-6F85685868C5}.Debug|x64.Build.0 = Debug|x64 + {F6804956-219B-4853-A162-6F85685868C5}.Debug|x86.ActiveCfg = Debug|Win32 + {F6804956-219B-4853-A162-6F85685868C5}.Debug|x86.Build.0 = Debug|Win32 + {F6804956-219B-4853-A162-6F85685868C5}.Release|x64.ActiveCfg = Release|x64 + {F6804956-219B-4853-A162-6F85685868C5}.Release|x64.Build.0 = Release|x64 + {F6804956-219B-4853-A162-6F85685868C5}.Release|x86.ActiveCfg = Release|Win32 + {F6804956-219B-4853-A162-6F85685868C5}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {EC1A5E13-B1F6-4EE9-A4CF-457F212A7707} + EndGlobalSection +EndGlobal diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/PosixThread_HelloWorld.cpp" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/PosixThread_HelloWorld.cpp" new file mode 100644 index 0000000..a51c3a9 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/PosixThread_HelloWorld.cpp" @@ -0,0 +1,36 @@ +// PosixThread_HelloWorld.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 +// + +#include "pch.h" +#include +#include + +using namespace std; + + +int main() +{ + pthread_t t1; + + pthread_attr_t attr; + + size_t stack_size = 512 * 1000; + + pthread_attr_setstacksize(&attr, stack_size); + + // 创建 t1 线程,并且将执行对象指向 void* helloWorld(void* ptr); + int result = pthread_create(&t1,&attr, helloWorld, NULL); + + + pthread_join(t1, NULL); + + return EXIT_SUCCESS; +} + +void* helloWorld(void* ptr) { + pthread_t t = pthread_self(); + //printf("Thread - Hello World \n"); + cout << "Hello World" << endl; + return NULL; +} + diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/PosixThread_HelloWorld.vcxproj" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/PosixThread_HelloWorld.vcxproj" new file mode 100644 index 0000000..2dabff4 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/PosixThread_HelloWorld.vcxproj" @@ -0,0 +1,171 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {F6804956-219B-4853-A162-6F85685868C5} + Win32Proj + PosixThreadHelloWorld + 10.0.17763.0 + + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + E:\software\dev\CPP\pthreads-w32-2-9-1\Pre-built.2\include;$(IncludePath) + E:\software\dev\CPP\pthreads-w32-2-9-1\Pre-built.2\lib;$(LibraryPath) + + + true + + + false + + + false + + + + Use + Level3 + Disabled + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + E:\software\dev\CPP\pthreads-w32-2-9-1\Pre-built.2\lib\x86\pthreadVC2.lib;%(AdditionalDependencies) + + + + + Use + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + + + + + Use + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + true + true + + + + + Use + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + true + true + + + + + + + + Create + Create + Create + Create + + + + + + + \ No newline at end of file diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/PosixThread_HelloWorld.vcxproj.filters" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/PosixThread_HelloWorld.vcxproj.filters" new file mode 100644 index 0000000..1dcd5b5 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/PosixThread_HelloWorld.vcxproj.filters" @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 头文件 + + + + + 源文件 + + + 源文件 + + + \ No newline at end of file diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/PosixThread_HelloWorld.vcxproj.user" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/PosixThread_HelloWorld.vcxproj.user" new file mode 100644 index 0000000..be25078 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/PosixThread_HelloWorld.vcxproj.user" @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/pch.cpp" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/pch.cpp" new file mode 100644 index 0000000..8eb50d0 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/pch.cpp" @@ -0,0 +1,5 @@ +// pch.cpp: 与预编译标头对应的源文件;编译成功所必需的 + +#include "pch.h" + +// 一般情况下,忽略此文件,但如果你使用的是预编译标头,请保留它。 diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/pch.h" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/pch.h" new file mode 100644 index 0000000..3dc472d --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_HelloWorld/PosixThread_HelloWorld/pch.h" @@ -0,0 +1,16 @@ +// 入门提示: +// 1. 使用解决方案资源管理器窗口添加/管理文件 +// 2. 使用团队资源管理器窗口连接到源代码管理 +// 3. 使用输出窗口查看生成输出和其他消息 +// 4. 使用错误列表窗口查看错误 +// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 +// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件 + +#ifndef PCH_H +#define PCH_H + +// TODO: 添加要在此处预编译的标头 + +void* helloWorld(void* ptr); + +#endif //PCH_H diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync.sln" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync.sln" new file mode 100644 index 0000000..0d143e5 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync.sln" @@ -0,0 +1,31 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 15 +VisualStudioVersion = 15.0.28307.438 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "PosixThread_Sync", "PosixThread_Sync\PosixThread_Sync.vcxproj", "{186FD364-9C46-475A-B433-7C56CCA71E90}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {186FD364-9C46-475A-B433-7C56CCA71E90}.Debug|x64.ActiveCfg = Debug|x64 + {186FD364-9C46-475A-B433-7C56CCA71E90}.Debug|x64.Build.0 = Debug|x64 + {186FD364-9C46-475A-B433-7C56CCA71E90}.Debug|x86.ActiveCfg = Debug|Win32 + {186FD364-9C46-475A-B433-7C56CCA71E90}.Debug|x86.Build.0 = Debug|Win32 + {186FD364-9C46-475A-B433-7C56CCA71E90}.Release|x64.ActiveCfg = Release|x64 + {186FD364-9C46-475A-B433-7C56CCA71E90}.Release|x64.Build.0 = Release|x64 + {186FD364-9C46-475A-B433-7C56CCA71E90}.Release|x86.ActiveCfg = Release|Win32 + {186FD364-9C46-475A-B433-7C56CCA71E90}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {D6EDBBD3-9280-49C2-9BCB-2A6D087E8082} + EndGlobalSection +EndGlobal diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/PosixThread_Sync.cpp" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/PosixThread_Sync.cpp" new file mode 100644 index 0000000..a910ae8 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/PosixThread_Sync.cpp" @@ -0,0 +1,82 @@ +// PosixThread_Sync.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。 +// + +#include "pch.h" +#include +#include + +int counter = 0; + +// 定义互斥对象 +pthread_mutex_t mutex1 = PTHREAD_MUTEX_INITIALIZER; + +// 定义条件变量 +pthread_cond_t condition_var = PTHREAD_COND_INITIALIZER; + +void* addCounter(void* ptr); + +void* minusCounter(void* ptr); + +int main() +{ + pthread_t t1; + pthread_t t2; + + pthread_create(&t1, NULL, addCounter, NULL); + pthread_create(&t2, NULL, minusCounter, NULL); + + + pthread_join(t1, NULL); + pthread_join(t2, NULL); + + std::cout << "Counter : " << counter << std::endl; + + return EXIT_SUCCESS; +} + +void* minusCounter(void* ptr) { + for (int i=0;i<100;i++) { + + // lock 加锁 + pthread_mutex_lock(&mutex1); + std::cout << "minusCounter - Before Counter : " << counter << std::endl; + + if (counter < 9 && counter > 1) { // 消费数据,唤起生产者线程 + counter--; + pthread_cond_signal(&condition_var); + } + + if (counter < 1) { // 当数据不足时,阻塞当前消费者线程 + pthread_cond_wait(&condition_var, &mutex1); + } + + + std::cout << "minusCounter - After Counter : " << counter << std::endl; + // unlock 解锁 + pthread_mutex_unlock(&mutex1); + } + return NULL; +} + +void* addCounter(void* ptr) { + for (int i = 0; i < 100; i++) { + // lock 加锁 + pthread_mutex_lock(&mutex1); + std::cout << "addCounter - Before Counter : " << counter << std::endl; + + if (counter < 9) { // 当数据不到阈值时,唤起当前消费者线程 + counter++; + pthread_cond_signal(&condition_var); + } + + if (counter > 9) { // 当数据达到阈值时,阻塞当前生产者线程 + pthread_cond_wait(&condition_var, &mutex1); + } + + std::cout << "addCounter - After Counter : " << counter << std::endl; + // unlock 解锁 + pthread_mutex_unlock(&mutex1); + } + return NULL; +} + diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/PosixThread_Sync.vcxproj" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/PosixThread_Sync.vcxproj" new file mode 100644 index 0000000..6308aa2 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/PosixThread_Sync.vcxproj" @@ -0,0 +1,171 @@ + + + + + Debug + Win32 + + + Release + Win32 + + + Debug + x64 + + + Release + x64 + + + + 15.0 + {186FD364-9C46-475A-B433-7C56CCA71E90} + Win32Proj + PosixThreadSync + 10.0.17763.0 + + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + Application + true + v141 + Unicode + + + Application + false + v141 + true + Unicode + + + + + + + + + + + + + + + + + + + + + true + E:\software\dev\CPP\pthreads-w32-2-9-1\Pre-built.2\include;$(IncludePath) + E:\software\dev\CPP\pthreads-w32-2-9-1\Pre-built.2\dll\x86;$(LibraryPath) + + + true + + + false + + + false + + + + Use + Level3 + Disabled + true + WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + E:\software\dev\CPP\pthreads-w32-2-9-1\Pre-built.2\lib\x86\pthreadVC2.lib;%(AdditionalDependencies) + + + + + Use + Level3 + Disabled + true + _DEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + + + + + Use + Level3 + MaxSpeed + true + true + true + WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + true + true + + + + + Use + Level3 + MaxSpeed + true + true + true + NDEBUG;_CONSOLE;%(PreprocessorDefinitions) + true + pch.h + + + Console + true + true + true + + + + + + + + Create + Create + Create + Create + + + + + + + \ No newline at end of file diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/PosixThread_Sync.vcxproj.filters" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/PosixThread_Sync.vcxproj.filters" new file mode 100644 index 0000000..19b500b --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/PosixThread_Sync.vcxproj.filters" @@ -0,0 +1,30 @@ + + + + + {4FC737F1-C7A5-4376-A066-2A32D752A2FF} + cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx + + + {93995380-89BD-4b04-88EB-625FBE52EBFB} + h;hh;hpp;hxx;hm;inl;inc;ipp;xsd + + + {67DA6AB6-F800-4c08-8B7A-83BB121AAD01} + rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms + + + + + 头文件 + + + + + 源文件 + + + 源文件 + + + \ No newline at end of file diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/PosixThread_Sync.vcxproj.user" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/PosixThread_Sync.vcxproj.user" new file mode 100644 index 0000000..be25078 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/PosixThread_Sync.vcxproj.user" @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/pch.cpp" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/pch.cpp" new file mode 100644 index 0000000..8eb50d0 --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/pch.cpp" @@ -0,0 +1,5 @@ +// pch.cpp: 与预编译标头对应的源文件;编译成功所必需的 + +#include "pch.h" + +// 一般情况下,忽略此文件,但如果你使用的是预编译标头,请保留它。 diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/pch.h" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/pch.h" new file mode 100644 index 0000000..69bf59d --- /dev/null +++ "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\344\273\243\347\240\201/segmentfault/visual-studio-workspace/PosixThread_Sync/PosixThread_Sync/pch.h" @@ -0,0 +1,14 @@ +// 入门提示: +// 1. 使用解决方案资源管理器窗口添加/管理文件 +// 2. 使用团队资源管理器窗口连接到源代码管理 +// 3. 使用输出窗口查看生成输出和其他消息 +// 4. 使用错误列表窗口查看错误 +// 5. 转到“项目”>“添加新项”以创建新的代码文件,或转到“项目”>“添加现有项”以将现有代码文件添加到项目 +// 6. 将来,若要再次打开此项目,请转到“文件”>“打开”>“项目”并选择 .sln 文件 + +#ifndef PCH_H +#define PCH_H + +// TODO: 添加要在此处预编译的标头 + +#endif //PCH_H diff --git "a/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\347\254\254\345\233\233\346\234\237/\347\254\254\344\270\200\350\212\202 POSIX Thread/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215\347\263\273\345\210\227 \347\254\254\345\233\233\346\234\237 \347\254\254\344\270\200\350\212\202 \343\200\212POSIX Thread\343\200\213.pdf" "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\347\254\254\345\233\233\346\234\237/\347\254\254\344\270\200\350\212\202 POSIX Thread/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215\347\263\273\345\210\227 \347\254\254\345\233\233\346\234\237 \347\254\254\344\270\200\350\212\202 \343\200\212POSIX Thread\343\200\213.pdf" new file mode 100644 index 0000000..5e05390 Binary files /dev/null and "b/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215/\347\254\254\345\233\233\346\234\237/\347\254\254\344\270\200\350\212\202 POSIX Thread/\343\200\214\344\270\200\345\205\245 Java \346\267\261\344\274\274\346\265\267 \343\200\215\347\263\273\345\210\227 \347\254\254\345\233\233\346\234\237 \347\254\254\344\270\200\350\212\202 \343\200\212POSIX Thread\343\200\213.pdf" differ