Parallel calculations: Difference between revisions

Added FreeBASIC
m (syntax highlighting fixup automation)
(Added FreeBASIC)
 
(One intermediate revision by one other user not shown)
Line 830:
Thread 0: 15780709 7 17 132611
12878611 have the Largest factor: 47</pre>
 
=={{header|FreeBASIC}}==
FreeBASIC does not have native support for parallel or multithreaded programming.
However, you can use external C libraries that provide multithreading functionality,
such as the POSIX threading library (pthreads) or the Windows threading library.
 
Here's a basic example of how you could use the pthreads library in FreeBASIC:
<syntaxhighlight lang="vbnet">#ifdef __FB_WIN32__
' ... instructions only for Win ...
#Include "windows.bi"
Function ThreadFunc As Dword Cdecl Alias "ThreadFunc"(param As Any Ptr) Export
Print "Thread running"
Function = 0
End Function
Dim As HANDLE thread
Dim As Dword threadId
thread = CreateThread(NULL, 0, @ThreadFunc, NULL, 0, @threadId)
If thread = NULL Then
Print "Error creating thread"
Sleep
End 1
End If
WaitForSingleObject(thread, INFINITE)
#endif
 
#ifdef __FB_LINUX__
' ... instructions only for Linux ...
#Include "crt/pthread.bi"
Function ThreadFunc As Any Ptr Cdecl Alias "ThreadFunc"(param As Any Ptr) Export
Print "Thread running"
Function = 0
End Function
Dim As pthread_t thread
If pthread_create(@thread, NULL, @ThreadFunc, NULL) <> 0 Then
Print "Error creating thread"
Sleep
End 1
End If
pthread_join(thread, NULL)
#endif
 
Print "Thread finished"
 
Sleep</syntaxhighlight>
 
=={{header|Go}}==
Line 2,698 ⟶ 2,751:
 
However, it's possible for a suitable host on a suitable machine to run multiple VMs in parallel as the following example, with a C host, shows when run on a machine with four cores. Four VMs are used each of which runs on its own thread.
<syntaxhighlight lang="ecmascriptwren">/* parallel_calculationsParallel_calculations.wren */
 
import "./math" for Int
Line 2,818 ⟶ 2,871:
config.loadModuleFn = &loadModule;
const char* module = "main";
const char* fileName = "parallel_calculationsParallel_calculations.wren";
char *script = readFile(fileName);
 
2,130

edits